package com.kun.comic;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;

import com.kun.comic.extra.NavigationDrawerCallbacks;
import com.kun.comic.extra.Ultils;
import com.kun.comic.fragment.HomeFragment;
import com.kun.comic.fragment.ListChapFragment;
import com.kun.comic.fragment.NavigationDrawerFragment;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.startapp.android.publish.StartAppAd;
import com.startapp.android.publish.StartAppSDK;

public class MainActivity extends ActionBarActivity implements
		NavigationDrawerCallbacks {

	/**
	 * Fragment managing the behaviors, interactions and presentation of the
	 * navigation drawer.
	 */
	private NavigationDrawerFragment mNavigationDrawerFragment;
	private Toolbar mToolbar;
	private Fragment mContent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
		setSupportActionBar(mToolbar);

		StartAppSDK.init(this, "103853684", "203012413", true);
		StartAppAd.showSplash(this, savedInstanceState);

		mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager()
				.findFragmentById(R.id.fragment_drawer);

		// Set up the drawer.
		mNavigationDrawerFragment.setup(R.id.fragment_drawer,
				(DrawerLayout) findViewById(R.id.drawer), mToolbar);
		// populate the navigation drawer
		// mNavigationDrawerFragment
		// .setUserData("", "", BitmapFactory.decodeResource(
		// getResources(), R.drawable.avatar));
		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_launcher)
				.cacheInMemory(false).showImageOnFail(R.drawable.ic_noimage)
				.showImageOnLoading(R.drawable.ic_load)
				.resetViewBeforeLoading(false).cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.ARGB_8888).build();
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getApplicationContext()).defaultDisplayImageOptions(options)
				.build();
		ImageLoader.getInstance().init(config);
		//
		// final DatabaseHandler db = new
		// DatabaseHandler(getApplicationContext());
		// try {
		// db.createDataBase();
		// } catch (IOException ex) {
		// Log.e("Get book, news cate ", ex.getMessage());
		// }
		//
		// SharedPreferences setting = getSharedPreferences("setting",
		// MODE_PRIVATE);
		//
		// Editor editor = setting.edit();
		// editor.putString("Order", "DESC");
		// editor.commit();

	}

	@Override
	public void onNavigationDrawerItemSelected(int position) {
		switch (position) {
		case 0:
			switchContent(new HomeFragment());
			break;
		case 1:
			switchContent(new ListChapFragment(1));
			break;
		case 2:
			switchContent(new ListChapFragment(2));
			break;
		case 3:
			switchContent(new ListChapFragment(3));
			break;
		case 4:
			switchContent(new ListChapFragment(4));
			break;
		case 5:
			switchContent(new ListChapFragment(5));
			break;
		case 6:
			switchContent(new ListChapFragment(6));
			break;
		case 7:
			switchContent(new ListChapFragment(7));
			break;
		case 8:
			switchContent(new ListChapFragment(8));
			break;
		case 9:
			switchContent(new ListChapFragment(9));
			break;
		default:
			break;
		}
	}

	@Override
	protected void onDestroy() {
		Ultils.DimissDialog();
		super.onDestroy();
	}

	public void switchContent(final Fragment fragment) {
		mContent = fragment;
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.container, mContent).commit();
	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// if (!mNavigationDrawerFragment.isDrawerOpen()) {
	//
	// getMenuInflater().inflate(R.menu.main, menu);
	// return true;
	// }
	// return super.onCreateOptionsMenu(menu);
	// }

	// @Override
	// public boolean onOptionsItemSelected(MenuItem item) {
	// return super.onOptionsItemSelected(item);
	// }

	boolean doubleBackToExitPressedOnce = false;

	@Override
	public void onBackPressed() {
		if (mNavigationDrawerFragment.isDrawerOpen())
			mNavigationDrawerFragment.closeDrawer();
		else {
			if (doubleBackToExitPressedOnce) {
				super.onBackPressed();
				return;
			}

			this.doubleBackToExitPressedOnce = true;
			Toast.makeText(this, getString(R.string.confirm_exit),
					Toast.LENGTH_SHORT).show();

			new Handler().postDelayed(new Runnable() {

				@Override
				public void run() {
					doubleBackToExitPressedOnce = false;
				}
			}, 2000);
		}
	}

}